package com.findme.gui;

import com.findme.controller.ControllerUsuario;
import com.findme.findme.R;
import com.findme.model.Usuario;
import com.findme.uteis.testeConeccao;
import com.findme.uteis.alertaDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;


public class TelaLogin extends Activity {
	Usuario user;
    ProgressDialog pdl ;
	private EditText edtSenha;	
	private EditText edtLogin;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tela_login);
		
	    edtLogin = (EditText) findViewById(R.id.edtLogin);
		edtSenha = (EditText) findViewById(R.id.edtSenha);	
		
		edtLogin.setText("fred.nogueira.ti@gmail.com");
		edtSenha.setText("1234");
		
		ImageView  btnEntrada  = (ImageView) findViewById(R.id.imgVwEntrar);
		
		btnEntrada.setOnClickListener(new View.OnClickListener() {
			
		public void onClick(View v) {
			pdl= new ProgressDialog(TelaLogin.this); 
			pdl.setTitle(R.string.login_sistema);          
			pdl.setIcon(R.drawable.ic_launcher);
			pdl.setCancelable(false);	
			
			pdl = ProgressDialog.show(TelaLogin.this,TelaLogin.this.getString(R.string.app_name),TelaLogin.this.getString(R.string.progressLogin), false, true);
			
					
            user=null;
            
			/*new Thread() {
				public void run() {		*/				
	
						
			//CARREGA A CLASSE E EXECUTA CONTROLADOR	
			user = new Usuario();
			user.setEmail(edtLogin.getText().toString().trim());
			user.setSenha(edtSenha.getText().toString().trim());
					
			ControllerUsuario ct = new ControllerUsuario(user);
			user=ct.logar(TelaLogin.this);		

		    pdl.dismiss(); //encerra progress	
		    
			if(user!=null){//se logou com sucesso
				
			  //intent da activity tela principal
			  Intent i = new Intent(TelaLogin.this, TelaPrincipal.class);
 
			  //passa paremetro de usuario para a tela principal
			  Bundle params = new Bundle();
			  params.putSerializable("Usuario", user);
			  i.putExtras(params);
			  		  
			  finish();	    //fecha a tela de login			  			 	  
			  startActivityForResult(i, 1); //inicia a tela principal
			  
				     
			}
			else{
				alertaDialog alerta = new alertaDialog();
				alerta.ShoewMessage(3,TelaLogin.this,""); 								
			};
				        							
			/*}	
				
				
				
				
			}.start();*/
			
			
			/*if(user==null){
				alertaDialog alerta = new alertaDialog();
				alerta.ShoewMessage(3,TelaLogin.this,""); 
				alerta=null;
				
			}*/
			
							
		}/*fim click*/
		
		
		});		/*fim listener*/
		
		
		//nova conta
		ImageView  imgNovaConta  = (ImageView) findViewById(R.id.imgVwNovaConta);
		
		imgNovaConta.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
			  Intent i = new Intent(TelaLogin.this, TelaNovaConta.class);
			  startActivityForResult(i, 1);
			}
			
		});				
		
		//esqueceu senha
		ImageView  imgEsqueceuSenha  = (ImageView) findViewById(R.id.imgVwEsqueceuSenha);
		
		imgEsqueceuSenha.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
			  Intent i = new Intent(TelaLogin.this, TelaEsqueceuSenha.class);
			  startActivityForResult(i, 1);
			}
			
		});	
		
		
		/*	pro_handler = new Handler() {
		@Override
		 public void handleMessage(Message msg) {
			 super.handleMessage(msg);
			 if (progress >= MAX_PROGRESS) {
				 pdl.dismiss();
			 } else {
				 progress++; 
				 pdl.incrementProgressBy(1);
				 pro_handler.sendEmptyMessageDelayed(0, 100);

			 }
		 }	
		};*/
		
		
		
		
		
		
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// InflatimgBtnEntrare the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.tela_login, menu);
		return true;
	}
	

	
	
	
	


}
